/*
 * GrantRequestStatusChangeCompletedView.java
 *
 * Created on November 20, 2008, 10:11 AM
 */

package researchgrants.parts.GrantRequest.statusChanges;

import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import researchgrants.parts.GrantRequest.GrantRequest;
import researchgrants.parts.db.Db;

/**
 * A view to edit an existing or input a new GrantRequestStatusChangeCompleted
 * @author  DOStudent1
 */
public class GrantRequestStatusChangeCompletedView extends GrantRequestStatusChangeView {

    private Window parent;
    private final static int hgap=0;

    // defferentiate between new (to be saved) and existing StatusChangeCompleted
    private boolean isNew;

    // if existing StatusChangeCompleted, this is it
    private GrantRequestStatusChangeCompleted grantRequestStatusChangeCompleted;

    // if new StatusChangeCompleted, need this supplement data (to connect to upon save)
    private GrantRequest grantRequest;

    // global-style component
    private ActionListener savedActionListener;
    private ActionListener cancelActionListener;
    private JButton btnSaveAll, btnCancel;

    /** Will creates a new (empty) completion and connect it to the given grantRequest*/
    public GrantRequestStatusChangeCompletedView(Window parent, GrantRequest grantRequest, ActionListener savedActionListener, ActionListener cancelActionListener) {
//        super()
        initComponents();
        this.parent=parent;
        this.grantRequest=grantRequest;
        this.savedActionListener=savedActionListener;
        this.cancelActionListener=cancelActionListener;
        isNew=true;

        initDisplay();
    }

    /** displays an existing completion */
    public GrantRequestStatusChangeCompletedView(Window parent, GrantRequestStatusChangeCompleted grantRequestStatusChangeCompleted) {
        initComponents();
        this.parent=parent;
        this.grantRequestStatusChangeCompleted=grantRequestStatusChangeCompleted;
        isNew=false;

        initDisplay();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlID = new javax.swing.JPanel();
        lblCaptionId = new javax.swing.JLabel();
        lblId = new javax.swing.JLabel();
        pnlOptionalButtons = new javax.swing.JPanel();

        setName("Form"); // NOI18N
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));

        pnlID.setName("pnlID"); // NOI18N
        pnlID.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, hgap, 0));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(researchgrants.ResearchGrantsApp.class).getContext().getResourceMap(GrantRequestStatusChangeCompletedView.class);
        lblCaptionId.setText(resourceMap.getString("lblCaptionId.text")); // NOI18N
        lblCaptionId.setName("lblCaptionId"); // NOI18N
        pnlID.add(lblCaptionId);

        lblId.setText(resourceMap.getString("lblId.text")); // NOI18N
        lblId.setName("lblId"); // NOI18N
        pnlID.add(lblId);

        add(pnlID);

        pnlOptionalButtons.setName("pnlOptionalButtons"); // NOI18N
        pnlOptionalButtons.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));
        add(pnlOptionalButtons);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblCaptionId;
    private javax.swing.JLabel lblId;
    private javax.swing.JPanel pnlID;
    private javax.swing.JPanel pnlOptionalButtons;
    // End of variables declaration//GEN-END:variables

    // displays the content
    private void initDisplay() {
        // must init the button so it will be enabled or disabled by the other fields
        if (isNew) {
            btnSaveAll=new JButton("Save Completion");
            btnSaveAll.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    createNewCompletionFromFields();
                }
            });

            btnCancel=new JButton("Leave Unchanged");
            btnCancel.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    cancelActionListener.actionPerformed(e);
                }
            });
            
        }

        ///////////////////////////
        // Displays the content
        ///////////////////////////
    
        if (isNew) {
            lblId.setText("<Auto>");
        } else {
            lblId.setText(Integer.toString(grantRequestStatusChangeCompleted.getStatusChangeId()));
        }

        
        
        pnlOptionalButtons.removeAll();
        if (isNew) {
            pnlOptionalButtons.setLayout(new GridLayout());
            pnlOptionalButtons.add(btnSaveAll);
            pnlOptionalButtons.add(btnCancel);
        }
        
    }

    // Creates a new GrantRequestStatusChangeAward from the fields
    private void createNewCompletionFromFields() {
        try {
            int newStatusChangeId = GrantRequestStatusChangeCompleted.createNewStatusChangeForGrantRequest(grantRequest, GrantRequestStatusChangeCompleted.dbString);

            Connection conn = Db.openDbConnection();
            PreparedStatement statement = Db.createPreparedStatement(conn, "INSERT INTO tblGrantRequestsStatusChangesCompleted (GrantRquestStatusChangeID) VALUES (?)");

            statement.setInt(1, newStatusChangeId);
            
            statement.executeUpdate();
            statement.close();
            Db.closeDbConnection(conn);

            this.grantRequestStatusChangeCompleted=GrantRequestStatusChangeCompleted.getByStatusChangeId(newStatusChangeId);
            grantRequest.refreshData();
            
            savedActionListener.actionPerformed(new ActionEvent(this, 1, "Saved new Completion StatusChange"));

        } catch (SQLException ex) {
            Logger.getLogger(GrantRequestStatusChangeCompletedView.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }
}
